﻿using PHRClient.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace PHRClient
{
    public partial class History : Form
    {        
        public int userId = 0;
        public List<PatientData> userDataList = null;
        

        public History()
        {
            InitializeComponent();            

            userId = Validator.patient.PatientId;

            userDataList = PatientDataLoader.getUserDataById(userId);

            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.Weight));


            this.TopLevel = false;
            this.AutoScroll = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Dock = DockStyle.Fill;

        }

        //GEt the chart
        private Chart GetChart(PHR phr)
        {            
            bool enable3D = false;

            Chart chart = null;
            
            try
            {
                pnChart.Controls.Clear();
                WindowsCharting charting = new WindowsCharting();
                DataTable dt = new DataTable();
                dt = feedData(phr);                

                SeriesChartType chatType = SeriesChartType.Line;

                chart = charting.GenerateChart(dt, pnChart.Width, pnChart.Height, Color.Transparent, chatType);
                chart.ChartAreas[0].Area3DStyle.Enable3D = enable3D;
                //return chart;
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                //do nothing
            }
            return chart;
        }

        private DataTable feedData(PHR phr)
        {
            DataTable dtDataPoints = null;

            switch (phr)
            {
                case PHR.BloodPressure: 
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "SYSTEMIC BP", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        DataColumn yaxis2 = new DataColumn() { ColumnName = "DIASTOLIC BP", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis2);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.BloodPressureSys;
                            workRow[2] = ud.BloodPressureDia;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }

                case PHR.BodyTemp:
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "Body Temperature", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.BodyTemp;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }
                case PHR.Cholestrol: 
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "Cholestrol", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        DataColumn yaxis2 = new DataColumn() { ColumnName = "Triglycerides", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis2);

                        DataColumn yaxis3 = new DataColumn() { ColumnName = "HDL", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis3);

                        DataColumn yaxis4 = new DataColumn() { ColumnName = "LDL", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis4);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.CholestrolTri + ud.CholestrolHdl + ud.CholestrolLdl;
                            workRow[2] = ud.CholestrolTri;
                            workRow[3] = ud.CholestrolHdl;
                            workRow[4] = ud.CholestrolLdl;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }
                case PHR.Glucose:
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "Glucose", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.Glucose;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }
                case PHR.HeartRate:
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "Heart Rate", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        DataColumn yaxis2 = new DataColumn() { ColumnName = "Pulse Rate", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis2);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.HeartRate;
                            workRow[1] = ud.PulseRate;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }               
                case PHR.Weight: 
                    {
                        dtDataPoints = new DataTable();

                        DataColumn xaxis = new DataColumn() { ColumnName = WindowsCharting.XAXIS, DataType = typeof(DateTime) };
                        dtDataPoints.Columns.Add(xaxis);

                        DataColumn yaxis1 = new DataColumn() { ColumnName = "Weight", DataType = typeof(int) };
                        dtDataPoints.Columns.Add(yaxis1);

                        foreach (PatientData ud in userDataList)
                        {
                            DataRow workRow = dtDataPoints.NewRow();

                            workRow[0] = Convert.ToDateTime(ud.RecordDate);
                            workRow[1] = ud.Weight;

                            dtDataPoints.Rows.Add(workRow);
                        }
                        return dtDataPoints;
                    }
          
                default:
                    {
                        return null;
                    }
            }           
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.Weight));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.HeartRate));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.Cholestrol));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.Glucose));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.BloodPressure));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            pnChart.Controls.Clear();
            pnChart.Controls.Add(GetChart(PHR.BodyTemp));
        }    
    }
}
